<template>
  <div class="caseComp">
    <div class="caseCompTitle">编辑查实情况</div>
    <el-form :model="info" :rules="rules" ref="ruleForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="是否解决：" prop="isFinish">
            <el-select
              v-model="info.isFinish"
              width="100%"
              class="caseCompTitleInput"
              placeholder="请选择"
            >
              <el-option
                v-for="item in resolveStatus"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="解决方式：" prop="finishType">
            <el-input
              v-model="info.finishType"
              clearable=""
              placeholder="请输入解决方式"
              class="caseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="涉及人数：" prop="involveCount">
            <el-input
              v-model="info.involveCount"
              clearable=""
              placeholder="请输入涉及人数"
              class="caseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="涉及金额：" prop="involveTotal">
            <el-input
              v-model="info.involveTotal"
              clearable=""
              placeholder="请输入涉及金额"
              class="caseCompTitleInput"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="caseCompBtn">
      <el-button type="primary" class="commonBut saveBtn" @click="save"
        >保存</el-button
      >
      <el-button type="default" class="commonBut" @click="finish"
        >取消</el-button
      >
    </div>
  </div>
</template>
<script>
export default {
  name: 'caseDepartment',
  props: {
    childData: {
      type: Object,
      required: true,
      default() {
        return {}
      },
    },
  },
  data() {
    var checkMoney = (rule, value, callback) => {
      if (!value) {
        callback()
      } else {
        var rePayTotal =
          /^(([1-9][0-9]*)|(([0]\.\d{1,2}|[1-9][0-9]*\.\d{1,2})))$/
        if (!rePayTotal.test(value) || value == 0.0 || value == 0.0) {
          callback(new Error('金额格式不正确,必须大于0,最多带有2位小数'))
        } else {
          if (value >= 100000000000) {
            callback(new Error('金额需小于1000亿'))
          } else {
            callback()
          }
        }
      }
    }
    return {
      resolveStatus: [
        { value: 'YES', name: '是' },
        { value: 'NO', name: '否' },
      ],
      resolveType: [
        { value: '1', name: '用人单位已支付' },
        { value: '2', name: '已劳动仲裁' },
        { value: '3', name: '人民法院诉讼' },
        { value: '4', name: '其他' },
      ],
      info: {
        id: '',
        isFinish: '',
        finishType: '',
        involveCount: '',
        involveTotal: '',
      },
      initInfo: {},
      rules: {
        isFinish: [
          { required: true, message: '请选择是否解决', trigger: 'change' },
        ],
        finishType: [
          { required: true, message: '请输入解决方式', trigger: 'change' },
        ],
        involveCount: [
          {
            pattern: /^[1-9]+[0-9]*$/,
            message: '请输入正整数',
            trigger: 'blur',
          },
        ],
        involveTotal: [{ validator: checkMoney, trigger: 'blur' }],
      },
    }
  },
  mounted() {
    const { id, isFinish, finishType, involveCount, involveTotal } =
      this.childData
    this.info = {
      id,
      isFinish: isFinish ? 'YES' : 'NO',
      finishType,
      involveCount,
      involveTotal,
    }
    this.initInfo = JSON.parse(JSON.stringify(this.info))
    this.$refs.ruleForm.resetFields()
  },
  methods: {
    save() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.isLoading = true
          let params = { id: this.info.id }
          let flag = true
          for (let item in this.info) {
            if (this.info[item] != this.initInfo[item]) {
              flag = false
              params[item] = this.info[item]
            }
          }
          if (flag) {
            this.finish()
            return false
          }
          const h = this.$createElement
          this.$msgbox({
            title: '提示',
            message: h('p', { style: 'position:relative;padding-left:40px' }, [
              h('i', {
                class: 'el-icon-warning',
                style:
                  'position:absolute;left:5px;top: 2px;color: orange;font-size:22px;margin-right:15px',
              }),
              h(
                'span',
                `修改受理状态为【已办结】后，将短信通知${this.childData.name}受理状态。`
              ),
            ]),
            showCancelButton: true,
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            beforeClose: (action, instance, done) => {
              if (action === 'confirm') {
                this.axios
                  .post('api-r/rightbackpay/expand/edit', params)
                  .then((res) => {
                    if (!res.data.hasError) {
                      this.axios
                        .post('api-r/rightbackpay/expand/editstatus', {
                          id: this.childData.id,
                        })
                        .then((res) => {
                          if (!res.data.hasError) {
                            this.$message({
                              type: 'success',
                              message: '操作成功',
                            })
                            this.isLoading = false
                            this.finish(1)
                            done()
                          } else {
                            this.$message({
                              type: 'warning',
                              message: res.data.errorMsg,
                            })
                            done()
                          }
                        })
                    } else {
                      this.$message({
                        type: 'warning',
                        message: res.data.errorMsg,
                      })
                      this.isLoading = false
                      done()
                    }
                  })
              } else {
                done()
              }
            },
          }).catch(() => {})
        } else {
          return false
        }
      })
    },
    finish(index) {
      this.$emit('callback', { success: index == 1 ? true : false })
    },
  },
}
</script>
<style lang="less" scoped>
.caseComp {
  border: 1px solid #dcdfe6;
  background-color: #f8f9fb;
  padding: 15px;
  &Title {
    color: #009588;
    font-size: 14px;
    line-height: 30px;
    margin-bottom: 12px;
  }
  &List {
    line-height: 36px;
    height: 36px;
    font-size: 14px;
    color: #444;
    margin-bottom: 12px;
    &Title {
      display: inline-block;
      width: 85px;
      text-align: right;
    }
    &Input {
      width: calc(100% - 95px);
    }
    .input {
      width: calc(100% - 170px);
    }
  }
  &Btn {
    text-align: center;
    margin: 24px 0 10px;
    .el-button {
      width: 120px;
    }
  }
  /deep/ .el-radio__input.is-checked + .el-radio__label {
    color: #009588 !important;
  }
  /deep/.el-radio__input.is-checked .el-radio__inner {
    background: #009588 !important;
    border-color: #009588 !important;
  }
}
.caseCompTitleInput {
  width: calc(100% - 20px);
}
</style>
